Conditions | 1 |
Total Lines | 228 |
Code Lines | 86 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /* |
||
39 | * A class to read and write wav files. |
||
40 | * @extends WaveFileReader |
||
41 | */ |
||
42 | export default class WaveFileParser extends WaveFileReader { |
||
43 | |||
44 | /** |
||
45 | * Return a byte buffer representig the WaveFileParser object as a .wav file. |
||
46 | * The return value of this method can be written straight to disk. |
||
47 | * @return {!Uint8Array} A wav file. |
||
48 | * @throws {Error} If bit depth is invalid. |
||
49 | * @throws {Error} If the number of channels is invalid. |
||
50 | * @throws {Error} If the sample rate is invalid. |
||
51 | */ |
||
52 | toBuffer() { |
||
53 | this.validateWavHeader(); |
||
54 | return this.writeWavBuffer_(); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Return the sample at a given index. |
||
59 | * @param {number} index The sample index. |
||
60 | * @return {number} The sample. |
||
61 | * @throws {Error} If the sample index is off range. |
||
62 | */ |
||
63 | getSample(index) { |
||
64 | index = index * (this.dataType.bits / 8); |
||
65 | if (index + this.dataType.bits / 8 > this.data.samples.length) { |
||
66 | throw new Error('Range error'); |
||
67 | } |
||
68 | return unpack( |
||
69 | this.data.samples.slice(index, index + this.dataType.bits / 8), |
||
70 | this.dataType); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Set the sample at a given index. |
||
75 | * @param {number} index The sample index. |
||
76 | * @param {number} sample The sample. |
||
77 | * @throws {Error} If the sample index is off range. |
||
78 | */ |
||
79 | setSample(index, sample) { |
||
80 | index = index * (this.dataType.bits / 8); |
||
81 | if (index + this.dataType.bits / 8 > this.data.samples.length) { |
||
82 | throw new Error('Range error'); |
||
83 | } |
||
84 | packTo(sample, this.dataType, this.data.samples, index); |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Validate the header of the file. |
||
89 | * @throws {Error} If bit depth is invalid. |
||
90 | * @throws {Error} If the number of channels is invalid. |
||
91 | * @throws {Error} If the sample rate is invalid. |
||
92 | * @ignore |
||
93 | * @protected |
||
94 | */ |
||
95 | validateWavHeader() { |
||
96 | this.validateBitDepth_(); |
||
97 | if (!validateNumChannels(this.fmt.numChannels, this.fmt.bitsPerSample)) { |
||
98 | throw new Error('Invalid number of channels.'); |
||
99 | } |
||
100 | if (!validateSampleRate( |
||
101 | this.fmt.numChannels, this.fmt.bitsPerSample, this.fmt.sampleRate)) { |
||
102 | throw new Error('Invalid sample rate.'); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Return a .wav file byte buffer with the data from the WaveFileParser object. |
||
108 | * The return value of this method can be written straight to disk. |
||
109 | * @return {!Uint8Array} The wav file bytes. |
||
110 | * @private |
||
111 | */ |
||
112 | writeWavBuffer_() { |
||
113 | this.uInt16_.be = this.container === 'RIFX'; |
||
114 | this.uInt32_.be = this.uInt16_.be; |
||
115 | /** @type {!Array<!Array<number>>} */ |
||
116 | let fileBody = [ |
||
117 | this.getJunkBytes_(), |
||
118 | this.getDs64Bytes_(), |
||
119 | this.getBextBytes_(), |
||
120 | this.getFmtBytes_(), |
||
121 | this.getFactBytes_(), |
||
122 | packString(this.data.chunkId), |
||
123 | pack(this.data.samples.length, this.uInt32_), |
||
124 | this.data.samples, |
||
125 | this.getCueBytes_(), |
||
126 | this.getSmplBytes_(), |
||
127 | this.getLISTBytes_() |
||
128 | ]; |
||
129 | /** @type {number} */ |
||
130 | let fileBodyLength = 0; |
||
131 | for (let i=0; i<fileBody.length; i++) { |
||
132 | fileBodyLength += fileBody[i].length; |
||
133 | } |
||
134 | /** @type {!Uint8Array} */ |
||
135 | let file = new Uint8Array(fileBodyLength + 12); |
||
136 | /** @type {number} */ |
||
137 | let index = 0; |
||
138 | index = packStringTo(this.container, file, index); |
||
139 | index = packTo(fileBodyLength + 4, this.uInt32_, file, index); |
||
140 | index = packStringTo(this.format, file, index); |
||
141 | for (let i=0; i<fileBody.length; i++) { |
||
142 | file.set(fileBody[i], index); |
||
143 | index += fileBody[i].length; |
||
144 | } |
||
145 | return file; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Return the bytes of the 'bext' chunk. |
||
150 | * @private |
||
151 | */ |
||
152 | getBextBytes_() { |
||
153 | /** @type {!Array<number>} */ |
||
154 | let bytes = []; |
||
155 | this.enforceBext_(); |
||
156 | if (this.bext.chunkId) { |
||
157 | this.bext.chunkSize = 602 + this.bext.codingHistory.length; |
||
158 | bytes = bytes.concat( |
||
159 | packString(this.bext.chunkId), |
||
160 | pack(602 + this.bext.codingHistory.length, this.uInt32_), |
||
161 | writeString(this.bext.description, 256), |
||
162 | writeString(this.bext.originator, 32), |
||
163 | writeString(this.bext.originatorReference, 32), |
||
164 | writeString(this.bext.originationDate, 10), |
||
165 | writeString(this.bext.originationTime, 8), |
||
166 | pack(this.bext.timeReference[0], this.uInt32_), |
||
167 | pack(this.bext.timeReference[1], this.uInt32_), |
||
168 | pack(this.bext.version, this.uInt16_), |
||
169 | writeString(this.bext.UMID, 64), |
||
170 | pack(this.bext.loudnessValue, this.uInt16_), |
||
171 | pack(this.bext.loudnessRange, this.uInt16_), |
||
172 | pack(this.bext.maxTruePeakLevel, this.uInt16_), |
||
173 | pack(this.bext.maxMomentaryLoudness, this.uInt16_), |
||
174 | pack(this.bext.maxShortTermLoudness, this.uInt16_), |
||
175 | writeString(this.bext.reserved, 180), |
||
176 | writeString( |
||
177 | this.bext.codingHistory, this.bext.codingHistory.length)); |
||
178 | } |
||
179 | return bytes; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Make sure a 'bext' chunk is created if BWF data was created in a file. |
||
184 | * @private |
||
185 | */ |
||
186 | enforceBext_() { |
||
187 | for (let prop in this.bext) { |
||
188 | if (this.bext.hasOwnProperty(prop)) { |
||
189 | if (this.bext[prop] && prop != 'timeReference') { |
||
190 | this.bext.chunkId = 'bext'; |
||
191 | break; |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | if (this.bext.timeReference[0] || this.bext.timeReference[1]) { |
||
196 | this.bext.chunkId = 'bext'; |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Return the bytes of the 'ds64' chunk. |
||
202 | * @return {!Array<number>} The 'ds64' chunk bytes. |
||
203 | * @private |
||
204 | */ |
||
205 | getDs64Bytes_() { |
||
206 | /** @type {!Array<number>} */ |
||
207 | let bytes = []; |
||
208 | if (this.ds64.chunkId) { |
||
209 | bytes = bytes.concat( |
||
210 | packString(this.ds64.chunkId), |
||
211 | pack(this.ds64.chunkSize, this.uInt32_), |
||
212 | pack(this.ds64.riffSizeHigh, this.uInt32_), |
||
213 | pack(this.ds64.riffSizeLow, this.uInt32_), |
||
214 | pack(this.ds64.dataSizeHigh, this.uInt32_), |
||
215 | pack(this.ds64.dataSizeLow, this.uInt32_), |
||
216 | pack(this.ds64.originationTime, this.uInt32_), |
||
217 | pack(this.ds64.sampleCountHigh, this.uInt32_), |
||
218 | pack(this.ds64.sampleCountLow, this.uInt32_)); |
||
219 | } |
||
220 | //if (this.ds64.tableLength) { |
||
221 | // ds64Bytes = ds64Bytes.concat( |
||
222 | // pack(this.ds64.tableLength, this.uInt32_), |
||
223 | // this.ds64.table); |
||
224 | //} |
||
225 | return bytes; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Return the bytes of the 'cue ' chunk. |
||
230 | * @return {!Array<number>} The 'cue ' chunk bytes. |
||
231 | * @private |
||
232 | */ |
||
233 | getCueBytes_() { |
||
234 | /** @type {!Array<number>} */ |
||
235 | let bytes = []; |
||
236 | if (this.cue.chunkId) { |
||
237 | /** @type {!Array<number>} */ |
||
238 | let cuePointsBytes = this.getCuePointsBytes_(); |
||
239 | bytes = bytes.concat( |
||
240 | packString(this.cue.chunkId), |
||
241 | pack(cuePointsBytes.length + 4, this.uInt32_), |
||
242 | pack(this.cue.dwCuePoints, this.uInt32_), |
||
243 | cuePointsBytes); |
||
244 | } |
||
245 | return bytes; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Return the bytes of the 'cue ' points. |
||
250 | * @return {!Array<number>} The 'cue ' points as an array of bytes. |
||
251 | * @private |
||
252 | */ |
||
253 | getCuePointsBytes_() { |
||
254 | /** @type {!Array<number>} */ |
||
255 | let points = []; |
||
256 | for (let i=0; i<this.cue.dwCuePoints; i++) { |
||
257 | points = points.concat( |
||
258 | pack(this.cue.points[i].dwName, this.uInt32_), |
||
259 | pack(this.cue.points[i].dwPosition, this.uInt32_), |
||
260 | packString(this.cue.points[i].fccChunk), |
||
261 | pack(this.cue.points[i].dwChunkStart, this.uInt32_), |
||
262 | pack(this.cue.points[i].dwBlockStart, this.uInt32_), |
||
263 | pack(this.cue.points[i].dwSampleOffset, this.uInt32_)); |
||
264 | } |
||
265 | return points; |
||
266 | } |
||
267 | |||
502 |